/* Copyright (c) 2011 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of Nordic Semiconductor. The use,
 * copying, transfer or disclosure of such information is prohibited except by express written
 * agreement with Nordic Semiconductor.
 *
 */
/**
  @addtogroup BLE_GATTC Generic Attribute Profile (GATT) Client.
  @{
  @brief  Definitions and prototypes for the GATTC interface.
 */

#ifndef BLE_GATTC_H__
#define BLE_GATTC_H__

#include "ble_types.h"
#include "ble_ranges.h"

/** @brief Only the default MTU size of 23 is currently supported. */
#define GATT_RX_MTU 23


/**@brief GATTC API SVC numbers. */
enum
{
  SVC_BLE_GATTC_MTU_EXCHANGE  = BLE_GATTC_SVC_BASE,    /**< MTU Exchange. */
  SVC_BLE_GATTC_PRIMARY_SVC_DISCOVERY_START,           /**< Start Primary Service Discovery. */
  SVC_BLE_GATTC_PRIMARY_SVC_DISCOVERY_CONTINUE,        /**< Continue Primary Service Discovery. */
  SVC_BLE_GATTC_RELATIONSHIP_DISCOVERY_START,          /**< Start Relationship Discovery. */
  SVC_BLE_GATTC_RELATIONSHIP_DISCOVERY_CONTINUE,       /**< Continue Relationship Discovery. */
  SVC_BLE_GATTC_CHAR_DISCOVERY_START,                  /**< Start Characteristic Discovery. */
  SVC_BLE_GATTC_CHAR_DISCOVERY_CONTINUE,               /**< Continue Characteristic Discovery. */
  SVC_BLE_GATTC_CHAR_DESC_DISCOVERY_START,             /**< Start Characteristic Descriptor Discovery. */
  SVC_BLE_GATTC_CHAR_DESC_DISCOVERY_CONTINUE,          /**< Continue Characteristic Descriptor Discovery. */
  SVC_BLE_GATTC_CHAR_VALUE_READ_BY_UUID_START,         /**< Start Read Characteristic Value by UUID. */
  SVC_BLE_GATTC_CHAR_VALUE_READ_BY_UUID_CONTINUE,      /**< Continue Read Characteristic Value by UUID. */
  SVC_BLE_GATTC_READ_START,                            /**< Start a generic read. */
  SVC_BLE_GATTC_READ_CONTINUE,                         /**< Continue a generic read. */
  SVC_BLE_GATTC_WRITE_START,                           /**< Start a write operation. */
  SVC_BLE_GATTC_WRITE_CONTINUE,                        /**< Continue a write operation. */
  SVC_BLE_GATTC_HVC                                    /**< Handle Value Confirmation. */
};


/**@brief First/last handle for an operation. */
typedef struct
{
  uint16_t          first_handle;
  uint16_t          last_handle;
} ble_gatt_handle_range_t;


/**@brief GATT service. */
typedef struct
{
  ble_uuid_t              svc_uuid;
  ble_gatt_handle_range_t handle_range;
} ble_gatt_svc_t;


/**@brief  GATT include. */
typedef struct
{
  uint16_t          handle;
  ble_gatt_svc_t    included_svc;
} ble_gatt_include_t;


/**@brief GATT characteristic. */
typedef struct
{
  ble_uuid_t        uuid;
  uint8_t           properties;
  uint16_t          handle_decl;
  uint16_t          handle_value;
} ble_gatt_char_t;


/**@brief GATT attribute. */
typedef struct
{
  ble_uuid_t        uuid;
  uint16_t          handle;
 } ble_gatt_desc_t;


/**@brief List of uuids. */
typedef struct
{
  uint8_t           uuid_cnt;
  ble_uuid_t*       p_svc_uuids;
} ble_uuid_list_t;


/** @defgroup BLE_GATTC_WRITE_OP GATT Client Write operations.
 * @{ */
#define BLE_GATTC_WRITE_OP_WWR   0x00  /** Write Without Response. */
#define BLE_GATTC_WRITE_OP_SWWR  0x01  /** Signed Write Without Response. */
#define BLE_GATTC_WRITE_OP_WCV   0x02  /** Write Characteristic Value (Write Request). */
#define BLE_GATTC_WRITE_OP_WL    0x03  /** Write Long. */
#define BLE_GATTC_WRITE_OP_RW    0x04  /** Reliable Write. */
/** @} */


/**@brief Write Parameters. */
typedef struct
{
  uint8_t    write_op;                 /**< Write Operation to be performed see BLE_GATTC_WRITE_OP. */
  uint16_t   handle;                   /**< Handle to the attribute to be written. */
  uint16_t   offset;                   /**< Offset in bytes. */
  uint16_t   len;                      /**< Length of data in bytes. */
  uint8_t*   p_value;                  /**< Pointer to the value data. */
} ble_gattc_write_params_t;


/**
 * @brief GATT Client Event IDs.
 */
enum
{
  BLE_GATTC_EVT_MTU_XCHG_RSP  = BLE_GATTC_EVT_BASE,  /**< MTU Exchange. */
  BLE_GATTC_EVT_PRIM_SVC_DISC_RSP,     /**< Primary Service Discovery event.  */
  BLE_GATTC_EVT_REL_DISC_RSP,          /**< Relationship Discovery event. */
  BLE_GATTC_EVT_CHAR_DISC_RSP,         /**< Characteristic Discovery event. */
  BLE_GATTC_EVT_DESC_DISC_RSP,         /**< Descriptor Discovery event. */
  BLE_GATTC_EVT_READ_BY_UUID_RSP,      /**< Read By UUID event. */
  BLE_GATTC_EVT_READ_RSP,              /**< Read event. */
  BLE_GATTC_EVT_WRITE_RSP,             /**< Write event. */
  BLE_GATTC_EVT_HVX                    /**< Handle Value Notification or Indication event. */
};


typedef struct
{
  uint16_t att_mtu;                    /**< Negotiated MTU. */
 } ble_gattc_evt_mtu_xchg_rsp;


typedef struct
{
  uint8_t             count;
  ble_gatt_svc_t*     p_svcs;
} ble_gattc_evt_prim_svc_disc_rsp_t;


/**@brief GATT relationship parameter. */
typedef struct
{
  uint8_t count;
  ble_gatt_include_t* p_includes;
} ble_gattc_evt_rel_disc_rsp_t;


/**@brief GATT characteristic parameter. */
typedef struct
{
  uint8_t             count;
  ble_gatt_char_t*    p_chars;
} ble_gattc_evt_char_disc_rsp_t;


/**@brief GATT characteristic parameter. */
typedef struct
{
  uint8_t             count;
  ble_gatt_desc_t*    p_desc;
} ble_gattc_evt_desc_disc_rsp_t;


/**@brief GATT read by UUID parameter. */
typedef struct
{
  uint8_t             dlen;
  uint8_t*            p_data;
} ble_gattc_evt_read_by_uuid_rsp_t;


/* GATT write by UUID parameter. */
#define ble_gattc_evt_read_rsp_t  ble_gattc_evt_read_by_uuid_rsp_t


/**@brief GATT write parameter. */
typedef struct
{
  uint8_t             write_op;
} ble_gattc_evt_write_rsp_t;


/**@brief GATT handle indication parameter. */
typedef struct
{
  uint8_t             indication;
  uint16_t            handle;
  uint8_t             dlen;
  uint8_t*            p_data;
 } ble_gattc_evt_hvx_t;


/**@brief GATTC event type. */
typedef struct
{
  uint16_t            conn_handle;                /**< Connection Handle  on which event occured. */
  uint8_t             gatt_status;                /**< GATT status code for the operation. */
  union
  {
    ble_gattc_evt_mtu_xchg_rsp         mtu_xchg_rsp;
    ble_gattc_evt_prim_svc_disc_rsp_t  prim_svc_disc_rsp;
    ble_gattc_evt_rel_disc_rsp_t       rel_disc_rsp;
    ble_gattc_evt_char_disc_rsp_t      char_disc_rsp;
    ble_gattc_evt_desc_disc_rsp_t      desc_disc_rsp;
    ble_gattc_evt_read_by_uuid_rsp_t   read_by_uuid_rsp;
    ble_gattc_evt_read_rsp_t           read_rsp;
    ble_gattc_evt_write_rsp_t          write_rsp;
    ble_gattc_evt_hvx_t hvx;
   } params;
 } ble_gattc_evt_t;


/**@brief Initiate a GATT MTU Exchange procedure.
 * @note Not yet implemented.
 *
 * @details This function will trigger an MTU exchange with the value defined in GATT_RX_MTU if defined, the default ATT MTU otherwise.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_MTU_EXCHANGE) ble_gattc_mtu_exchange(uint16_t conn_handle);


/**@brief Initiate a GATT Primary Service Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function starts the procedure, use @ref ble_gattc_primary_svc_discovery_continue after the first callback to proceed.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] p_svc_uuid_list Pointer to a list of service UUIDs to be found. If the list is empty, all primary services will be returned.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_PRIMARY_SVC_DISCOVERY_START) ble_gattc_primary_svc_discovery_start(uint16_t conn_handle, ble_uuid_list_t const * const p_svc_uuid_list);


/**@brief Continue a GATT Primary Service Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function continues the procedure, use @ref ble_gattc_primary_svc_discovery_start to initiate it.
 *          This function may only be called once per result callback.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_PRIMARY_SVC_DISCOVERY_CONTINUE) ble_gattc_primary_svc_discovery_continue(uint16_t conn_handle);


/**@brief Initiate a GATT Relationship Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function starts the procedure, use @ref ble_gattc_relationship_discovery_continue after the first callback to proceed.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] p_handle_range A pointer to the range of handles to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_RELATIONSHIP_DISCOVERY_START) ble_gattc_relationship_discovery_start(uint16_t conn_handle, ble_gatt_handle_range_t const * const p_handle_range);


/**@brief Continue a GATT Relationship Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function continues the procedure, use @ref ble_gattc_relationship_discovery_start to initiate it.
 *          This function may only be called once per result callback.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_RELATIONSHIP_DISCOVERY_CONTINUE) ble_gattc_relationship_discovery_continue(uint16_t conn_handle);


/**@brief Initiate a GATT Characteristic Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function starts the procedure, use @ref ble_gattc_char_discovery_continue after the first callback to proceed.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] p_handle_range A pointer to the range of handles to perform this procedure on.
 * @param[in] p_char_uuid_list Pointer to a list of characteristic UUIDs to be found. If the list is empty, all characteristics will be returned.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_CHAR_DISCOVERY_START) ble_gattc_char_discovery_start(uint16_t conn_handle, ble_gatt_handle_range_t const * const p_handle_range, ble_uuid_list_t const * const p_char_uuid_list);


/**@brief Continue a GATT Characteristic Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function continues the procedure, use @ref ble_gattc_char_discovery_start to initiate it.
 *          This function may only be called once per result callback.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_CHAR_DISCOVERY_CONTINUE) ble_gattc_char_discovery_continue(uint16_t conn_handle);


/**@brief Initiate a GATT Characteristic Descriptor Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function starts the procedure, use @ref ble_gattc_char_desc_discovery_continue after the first callback to proceed.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] p_handle_range A pointer to the range of handles to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_CHAR_DESC_DISCOVERY_START) ble_gattc_char_desc_discovery_start(uint16_t conn_handle, ble_gatt_handle_range_t const * const p_handle_range);


/**@brief Continue a GATT Characteristic Descriptor Discovery procedure.
 * @note Not yet implemented.
 *
 * @details This function continues the procedure, use @ref ble_gattc_char_desc_discovery_start to initiate it.
 *          This function may only be called once per result callback.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_CHAR_DESC_DISCOVERY_CONTINUE) ble_gattc_char_desc_discovery_continue(uint16_t conn_handle);


/**@brief Initiate a GATT Read using Characteristic UUID procedure.
 * @note Not yet implemented.
 *
 * @details This function starts the procedure, use @ref ble_gattc_char_value_read_by_uuid_continue after the first callback to proceed.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] p_handle_range A pointer to the range of handles to perform this procedure on.
 * @param[in] p_uuid Pointer to a Characteristic value UUID to read.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_CHAR_VALUE_READ_BY_UUID_START) ble_gattc_char_value_read_by_uuid_start(uint16_t conn_handle, ble_gatt_handle_range_t const * const p_handle_range, ble_uuid_t const * const p_uuid);


/**@brief Continue a GATT Read using Characteristic UUID procedure.
 * @note Not yet implemented.
 *
 * @details This function continues the procedure, use @ref ble_gattc_char_value_read_by_uuid_start to initiate it.
 *          This function may only be called once per result callback.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_CHAR_VALUE_READ_BY_UUID_CONTINUE) ble_gattc_char_value_read_by_uuid_continue(uint16_t conn_handle);


/**@brief Initiate a GATT Read (Long, Multiple) Characteristic or Descriptor procedure.
 * @note Not yet implemented.
 *
 * @details This function starts the procedure, use @ref ble_gattc_read_continue after the first callback to proceed if required.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] p_handles A pointer to the handle(s) of the attribute(s) to be read.
 * @param[in] handle_count The number of handles in p_handles.
 * @param[in] offset Offset into the attribute value to be read.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_READ_START) ble_gattc_read_start(uint16_t conn_handle, uint16_t const * const p_handles, uint8_t handle_count, uint16_t offset);


/**@brief Continue a GATT Read Long Characteristic or Descriptor procedure.
 * @note Not yet implemented.
 *
 * @details This function continues the procedure, use @ref ble_gattc_read_start to initiate it.
 *          This function may only be called once per result callback.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] offset Offset into the attribute value to be read.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_READ_CONTINUE) ble_gattc_read_continue(uint16_t conn_handle, uint16_t offset);


/**@brief Perform a Write (Characteristic Value or Descriptor, with or without response, signed or not, long or reliable) procedure.
 * @note Not yet implemented.
 *
 * @details This function can perform all write procedures described in GATT.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] p_write_params A pointer to a write parameters structure.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_WRITE_START) ble_gattc_write_start(uint16_t conn_handle, ble_gattc_write_params_t const * const p_write_params);


/**@brief Continue a GATT Write Long or Reliable procedure.
 * @note Not yet implemented.
 *
 * @details This function continues the procedure, use @ref ble_gattc_read_start to initiate it.
 *          This function may only be called once per result callback.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on
 * @param[in] p_write_params A pointer to a write parameters structure.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_WRITE_CONTINUE) ble_gattc_write_continue(uint16_t conn_handle, ble_gattc_write_params_t const * const p_write_params);


/**@brief Send a Handle Value Confirmation to the server.
 * @note Not yet implemented.
 *
 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
 * @param[in] handle The handle of the attribute in the indication.
 * @return NRF_ERROR_NOT_SUPPORTED
 */
uint32_t SVCALL(SVC_BLE_GATTC_HVC) ble_gattc_hvc(uint16_t conn_handle, uint16_t handle);


#endif // BLE_GATTC_H__

/**
  @}
  @}
*/
